package com.cskaoyan.service;

import com.cskaoyan.bean.bo.wjjBO.*;
import com.cskaoyan.bean.marketBean.*;
import com.cskaoyan.bean.po.example.*;
import com.cskaoyan.bean.vo.wjjVO.*;
import com.cskaoyan.mapper.marketMapper.MarketAdMapper;
import com.cskaoyan.mapper.marketMapper.MarketCouponMapper;
import com.cskaoyan.mapper.marketMapper.MarketCouponUserMapper;
import com.cskaoyan.mapper.marketMapper.MarketTopicMapper;
import com.cskaoyan.utils.RandomStrUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wjj
 * @date 2022-12-29 10:31
 * @describe 推广模块的相关service功能实现
 */
@Transactional
@Service
public class PromotionServiceImpl implements PromotionService {

    @Autowired
    MarketAdMapper adMapper;

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketCouponUserMapper couponUserMapper;

    @Autowired
    MarketTopicMapper topicMapper;

    @Override
    public PromotionAdListVO selectMarketAd(PromotionAdListBO promotionAdListBO) {
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(promotionAdListBO.getPage(), promotionAdListBO.getLimit());
        //执行查询过程中拼接分页信息

        String orderBy = promotionAdListBO.getSort() + " " + promotionAdListBO.getOrder();

        PromotionAdListExamplePO promotionAdListExamplePO = new PromotionAdListExamplePO();
        promotionAdListExamplePO.setOrderBy(orderBy);
        if (promotionAdListBO.getName() != null) {
            if (!"".equals(promotionAdListBO.getName().trim())) {
                promotionAdListExamplePO.setName("%" + promotionAdListBO.getName() + "%");
            }
        }
        if (promotionAdListBO.getContent() != null) {
            if (!"".equals(promotionAdListBO.getContent().trim())) {
                promotionAdListExamplePO.setContent("%" + promotionAdListBO.getContent() + "%");
            }
        }

        List<MarketAd> marketAdList = adMapper.selectMarketAdByPageLimit(promotionAdListExamplePO);

        // 将查询结果作为有参构造方法的形参传入，可以获得PageInfo
        PageInfo<MarketAd> pageInfo = new PageInfo<>(marketAdList);
        PromotionAdListVO promotionAdListVO = new PromotionAdListVO();
        promotionAdListVO.setLimit(promotionAdListBO.getLimit());
        promotionAdListVO.setList(marketAdList);
        promotionAdListVO.setPage(promotionAdListBO.getPage());
        promotionAdListVO.setPages(pageInfo.getPages());
        promotionAdListVO.setTotal(pageInfo.getTotal());
        return promotionAdListVO;
    }

    @Override
    public PromotionAdCreateAdAndUpdateAdVO addNewAd(PromotionAdCreateNewAdBO newAd) {
        MarketAd ad = new MarketAd();
        ad.setContent(newAd.getContent());
        ad.setEnabled(newAd.getEnabled());
        ad.setLink(newAd.getLink());
        ad.setName(newAd.getName());
        ad.setPosition(newAd.getPosition());
        ad.setUrl(newAd.getUrl());
        Date date = new Date();
        ad.setAddTime(date);
        ad.setUpdateTime(date);
        int affectRows = adMapper.insertNewMarketAd(ad);

        PromotionAdCreateAdAndUpdateAdVO newAdVO = new PromotionAdCreateAdAndUpdateAdVO();
        newAdVO.setAddTime(ad.getAddTime());
        newAdVO.setContent(ad.getContent());
        newAdVO.setEnabled(ad.getEnabled());
        newAdVO.setId(ad.getId());
        newAdVO.setLink(ad.getLink());
        newAdVO.setName(ad.getName());
        newAdVO.setPosition(ad.getPosition());
        newAdVO.setUpdateTime(ad.getUpdateTime());
        newAdVO.setUrl(ad.getUrl());
        return newAdVO;
    }

    @Override
    public void deleteAd(PromotionAdDeleteBO promotionAdDeleteBO) {
        int affectRows = adMapper.deleteByPrimaryKey(promotionAdDeleteBO.getId());
    }

    @Override
    public MarketAd updateAd(PromotionAdUpdateBO promotionAdUpdateBO) {
        MarketAd marketAd = new MarketAd();
        marketAd.setAddTime(promotionAdUpdateBO.getAddTime());
        marketAd.setContent(promotionAdUpdateBO.getContent());
        marketAd.setEnabled(promotionAdUpdateBO.getEnabled());
        marketAd.setId(promotionAdUpdateBO.getId());
        marketAd.setLink(promotionAdUpdateBO.getLink());
        marketAd.setName(promotionAdUpdateBO.getName());
        marketAd.setPosition(promotionAdUpdateBO.getPosition());
        marketAd.setUpdateTime(new Date());
        marketAd.setUrl(promotionAdUpdateBO.getUrl());

        int affectRows = adMapper.updateByPrimaryKeySelective(marketAd);

        return marketAd;
    }

    @Override
    public PromotionAdCouponListVO selectCouponList(PromotionCouponListBO promotionCouponListBO) {
        //分页插件
        PageHelper.startPage(promotionCouponListBO.getPage(), promotionCouponListBO.getLimit());

        //将排序规则和排序的对象拼接
        String orderBy = promotionCouponListBO.getSort() + " " + promotionCouponListBO.getOrder();

        PromotionAdCouponListExamplePO couponListExamplePO = new PromotionAdCouponListExamplePO();
        couponListExamplePO.setOrderBy(orderBy);
        if (promotionCouponListBO.getName() != null) {
            if (!"".equals(promotionCouponListBO.getName().trim())) {
                couponListExamplePO.setName("%" + promotionCouponListBO.getName() + "%");
            }
        }
        couponListExamplePO.setType(promotionCouponListBO.getType());
        couponListExamplePO.setStatus(promotionCouponListBO.getStatus());

        List<MarketCoupon> marketCouponList = couponMapper.selectMarketCouponByAdListExample(couponListExamplePO);

        PageInfo<MarketCoupon> couponPageInfo = new PageInfo<>(marketCouponList);

        PromotionAdCouponListVO couponListVO = new PromotionAdCouponListVO();
        couponListVO.setLimit(promotionCouponListBO.getLimit());
        couponListVO.setList(marketCouponList);
        couponListVO.setPage(promotionCouponListBO.getPage());
        couponListVO.setPages(couponPageInfo.getPages());
        couponListVO.setTotal(couponPageInfo.getTotal());

        return couponListVO;
    }

    @Override
    public MarketCoupon addNewCoupon(PromotionCouponCreateBO couponCreateBO) {
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon.setDays(couponCreateBO.getDays());
        marketCoupon.setDesc(couponCreateBO.getDesc());
        marketCoupon.setDiscount(couponCreateBO.getDiscount());
        marketCoupon.setEndTime(couponCreateBO.getEndTime());
        marketCoupon.setGoodsType(couponCreateBO.getGoodsType());
        marketCoupon.setGoodsValue(couponCreateBO.getGoodsValue());
        marketCoupon.setLimit(couponCreateBO.getLimit());
        marketCoupon.setMin(couponCreateBO.getMin());
        marketCoupon.setName(couponCreateBO.getName());
        marketCoupon.setStartTime(couponCreateBO.getStartTime());
        marketCoupon.setStatus(couponCreateBO.getStatus());
        marketCoupon.setTag(couponCreateBO.getTag());
        marketCoupon.setTimeType(couponCreateBO.getTimeType());
        marketCoupon.setTotal(couponCreateBO.getTotal());
        marketCoupon.setType(couponCreateBO.getType());

        if (couponCreateBO.getType() == 2){
            //如果优惠券类型是兑换码的话,生成一个随机兑换码
            String code = RandomStrUtils.getRandomNickname(18);
            marketCoupon.setCode(code);
        }

        Date date = new Date();
        marketCoupon.setAddTime(date);
        marketCoupon.setUpdateTime(date);

        int affectRows = couponMapper.insertNewMarketCoupon(marketCoupon);

        return marketCoupon;
    }

    @Override
    public int deleteCoupon(Map map) {
        Integer id = (Integer) map.get("id");
        System.out.println(id);
        int affectRows = couponMapper.deleteByPrimaryKey(id);

        return affectRows;
    }

    @Override
    public MarketCoupon updateCoupon(PromotionCouponUpdateBO couponUpdateBO) {
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon.setAddTime(couponUpdateBO.getAddTime());
        marketCoupon.setDays(couponUpdateBO.getDays());
        marketCoupon.setDeleted(couponUpdateBO.getDeleted());
        marketCoupon.setDesc(couponUpdateBO.getDesc());
        marketCoupon.setDiscount(couponUpdateBO.getDiscount());
        marketCoupon.setEndTime(couponUpdateBO.getEndTime());
        marketCoupon.setGoodsType(couponUpdateBO.getGoodsType());
        marketCoupon.setGoodsValue(couponUpdateBO.getGoodsValue());
        marketCoupon.setId(couponUpdateBO.getId());
        marketCoupon.setLimit(couponUpdateBO.getLimit());
        marketCoupon.setMin(couponUpdateBO.getMin());
        marketCoupon.setName(couponUpdateBO.getName());
        marketCoupon.setStartTime(couponUpdateBO.getStartTime());
        marketCoupon.setStatus(couponUpdateBO.getStatus());
        marketCoupon.setTag(couponUpdateBO.getTag());
        marketCoupon.setTimeType(couponUpdateBO.getTimeType());
        marketCoupon.setTotal(couponUpdateBO.getTotal());
        marketCoupon.setType(couponUpdateBO.getType());
        marketCoupon.setUpdateTime(new Date());


        int affectRows = couponMapper.updateByPrimaryKey(marketCoupon);

        return marketCoupon;
    }

    @Override
    public MarketCoupon selectCouponById(Integer id) {

        MarketCoupon coupon = couponMapper.selectByPrimaryKey(id);

        return coupon;
    }

    @Override
    public PromotionAdCouponListUserVO selectCouponUserById(PromotionCouponListUserBO couponListUserBO) {
        //分页插件
        PageHelper.startPage(couponListUserBO.getPage(), couponListUserBO.getLimit());

        //将排序规则和排序的对象拼接
        String orderBy = couponListUserBO.getSort() + " " + couponListUserBO.getOrder();

        PromotionCouponListUserExamplePO couponListUserExamplePO = new PromotionCouponListUserExamplePO();
        couponListUserExamplePO.setOrderBy(orderBy);
        if (couponListUserBO.getUserId() != null) {
            couponListUserExamplePO.setUserId("%" + couponListUserBO.getUserId() + "%");
        }
        couponListUserExamplePO.setStatus(couponListUserBO.getStatus());
        couponListUserExamplePO.setCouponId(couponListUserBO.getCouponId());

        List<MarketCouponUser> couponUsers = couponUserMapper.selectMarketCouponUserByAdListExample(couponListUserExamplePO);

        PageInfo<MarketCouponUser> couponPageInfo = new PageInfo<>(couponUsers);

        PromotionAdCouponListUserVO couponListUserVO = new PromotionAdCouponListUserVO();
        couponListUserVO.setLimit(couponListUserBO.getLimit());
        couponListUserVO.setList(couponUsers);
        couponListUserVO.setPage(couponListUserBO.getPage());
        couponListUserVO.setPages(couponPageInfo.getPages());
        couponListUserVO.setTotal(couponPageInfo.getTotal());

        return couponListUserVO;

    }

    @Override
    public PromotionTopicVO selectTopic(PromotionTopicListBO topicListBO) {
        //分页插件
        PageHelper.startPage(topicListBO.getPage(), topicListBO.getLimit());

        //将排序规则和排序的对象拼接
        String orderBy = topicListBO.getSort() + " " + topicListBO.getOrder();

        PromotionTopicExamplePO topicExamplePO = new PromotionTopicExamplePO();
        topicExamplePO.setOrderBy(orderBy);
        if (topicListBO.getSubtitle() != null) {
            topicExamplePO.setSubtitle("%" + topicListBO.getSubtitle() + "%");
        }
        if (topicListBO.getTitle() != null) {
            topicExamplePO.setTitle("%" + topicListBO.getTitle() + "%");
        }

        List<MarketTopic> topics = topicMapper.selectMarketTopicByTopicExample(topicExamplePO);

        PageInfo<MarketTopic> couponPageInfo = new PageInfo<>(topics);

        PromotionTopicVO topicVO = new PromotionTopicVO();
        topicVO.setLimit(topicListBO.getLimit());
        topicVO.setList(topics);
        topicVO.setPage(topicListBO.getPage());
        topicVO.setPages(couponPageInfo.getPages());
        topicVO.setTotal(couponPageInfo.getTotal());

        return topicVO;
    }

    @Override
    public MarketTopic addTopic(PromotionTopicCreateBO topicCreateBO) {
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setContent(topicCreateBO.getContent());
        marketTopic.setGoods(topicCreateBO.getGoods());
        marketTopic.setPicUrl(topicCreateBO.getPicUrl());
        marketTopic.setPrice(topicCreateBO.getPrice());
        marketTopic.setReadCount(topicCreateBO.getReadCount());
        marketTopic.setSubtitle(topicCreateBO.getSubtitle());
        marketTopic.setTitle(topicCreateBO.getTitle());
        Date date = new Date();
        marketTopic.setAddTime(date);
        marketTopic.setUpdateTime(date);

        int affectRows = topicMapper.insertMarketTopic(marketTopic);

        return marketTopic;
    }

    @Override
    public void deleteTopic(PromotionTopicDeleteBO topicDeleteBO) {
        int affectRows = topicMapper.deleteByPrimaryKey(topicDeleteBO.getId());
    }

    @Override
    public void batchDeleteTopic(PromotionTopicBatchDeleteBO topicBatchDeleteBO) {
        List<Integer> ids = topicBatchDeleteBO.getIds();
        for (Integer id : ids) {
            int affectRows = topicMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public PromotionTopicReadVO readTopic(Integer id) {
        MarketTopic topic = topicMapper.selectTopicById(id);
        Integer[] goods = topic.getGoods();
        ArrayList<MarketGoods> goodsList = new ArrayList<>();
        for (Integer goodId : goods) {
            MarketGoods marketGoods = topicMapper.selectMarketGoodsById(goodId);
            goodsList.add(marketGoods);
        }

        PromotionTopicReadVO topicReadVO = new PromotionTopicReadVO();
        topicReadVO.setTopic(topic);
        topicReadVO.setGoodsList(goodsList);
        return topicReadVO;
    }

    @Override
    public MarketTopic updateTopic(PromotionTopicUpdateBO topicUpdateBO) {
        MarketTopic topic = new MarketTopic();
        topic.setContent(topicUpdateBO.getContent());
        topic.setDeleted(topicUpdateBO.getDeleted());
        topic.setGoods(topicUpdateBO.getGoods());
        topic.setId(topicUpdateBO.getId());
        topic.setPicUrl(topicUpdateBO.getPicUrl());
        topic.setPrice(topicUpdateBO.getPrice());
        topic.setReadCount(topicUpdateBO.getReadCount());
        topic.setSortOrder(topicUpdateBO.getSortOrder());
        topic.setSubtitle(topicUpdateBO.getTitle());
        Date date = new Date();
        topic.setUpdateTime(date);

        int affectRow = topicMapper.updateByPrimaryKeySelective(topic);

        return topic;
    }

    @Override
    public WJJUserCollectListVO selectCollect(WJJCollectListBO collectListBO) {
        //分页插件
        PageHelper.startPage(collectListBO.getPage(), collectListBO.getLimit());

        //将排序规则和排序的对象拼接
        String orderBy = collectListBO.getSort() + " " + collectListBO.getOrder();

        WJJUserCollectListExamplePO collectListExamplePO = new WJJUserCollectListExamplePO();
        collectListExamplePO.setOrderBy(orderBy);
        if (collectListBO.getUserId() != null) {
            collectListExamplePO.setUserId("%" + collectListBO.getUserId() + "%");
        }
        if (collectListBO.getValueId() != null) {
            collectListExamplePO.setValueId("%" + collectListBO.getValueId() + "%");
        }

        List<MarketCollect> marketCollects = topicMapper.selectMarketCollectExample(collectListExamplePO);

        PageInfo<MarketCollect> couponPageInfo = new PageInfo<>(marketCollects);

        WJJUserCollectListVO collectListVO = new WJJUserCollectListVO();
        collectListVO.setLimit(collectListBO.getLimit());
        collectListVO.setList(marketCollects);
        collectListVO.setPage(collectListBO.getPage());
        collectListVO.setPages(couponPageInfo.getPages());
        collectListVO.setTotal((int) couponPageInfo.getTotal());

        return collectListVO;

    }

    @Override
    public WJJFootprintListVO selectFootprint(WJJFootprintListBO footprintListBO) {
        //分页插件
        PageHelper.startPage(footprintListBO.getPage(), footprintListBO.getLimit());

        //将排序规则和排序的对象拼接
        String orderBy = footprintListBO.getSort() + " " + footprintListBO.getOrder();

        WJJFootprintExamplePO footprintExamplePO = new WJJFootprintExamplePO();
        footprintExamplePO.setOrderBy(orderBy);
        if (footprintListBO.getUserId() != null) {
            footprintExamplePO.setUserId("%" + footprintListBO.getUserId() + "%");
        }
        if (footprintListBO.getGoodsId() != null) {
            footprintExamplePO.setGoodsId("%" + footprintListBO.getGoodsId() + "%");
        }

        List<MarketFootprint> footprints = topicMapper.selectMarketFootprintExample(footprintExamplePO);

        PageInfo<MarketFootprint> couponPageInfo = new PageInfo<>(footprints);

        WJJFootprintListVO collectListVO = new WJJFootprintListVO();
        collectListVO.setLimit(footprintListBO.getLimit());
        collectListVO.setList(footprints);
        collectListVO.setPage(footprintListBO.getPage());
        collectListVO.setPages(couponPageInfo.getPages());
        collectListVO.setTotal((int) couponPageInfo.getTotal());

        return collectListVO;
    }
}
